package com.defysoft.waukids.notice;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;

import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.defysoft.waukids.BaseActivity;
import com.defysoft.waukids.R;

public class Notice_Main_A extends BaseActivity implements OnClickListener {
	private static final int FFC000 = 0;

	TextView school_Tv, class_Tv;
	int checkpage = 1;
	int checkFrag = 0;
	SharedPreferences member;
	Fragment fragment[] = new Fragment[2];
	String classcode = "10";
	private SharedPreferences check_notice;
	final int MASTER_TEACHER = 10; // 원장님.
	Toolbar toolbar;
	ImageView imageView;

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		Intent i = new Intent();
		if (resultCode == TOOLBAR_BACK || resultCode == NOTICE_EDIT) {
			if (data.getStringExtra("Mem_Rank").equals("2")) {// 선생님
				fragment[checkFrag]
						.onActivityResult(
								requestCode,
								resultCode,
								i.putExtra("Mem_Rank",
										data.getStringExtra("Mem_Rank")));
			} else {
				fragment[0]
						.onActivityResult(
								requestCode,
								resultCode,
								i.putExtra("Mem_Rank",
										data.getStringExtra("Mem_Rank")));
			}

		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_notice__main_);
		try {
			if (getIntent().getStringExtra("class") != null) {
				classcode = getIntent().getStringExtra("class");
			}
		} catch (Exception e) {
		}
		setStartData();
		setStartToolbar();
	}

	@Override
	public void setStartData() {
		super.setStartData();
		check_notice = getSharedPreferences("check_notice", MODE_PRIVATE);
		check_notice.edit().putInt("notice_count", 0).commit();

		member = getSharedPreferences("member", MODE_PRIVATE);
		class_Tv = (TextView) findViewById(R.id.NOTICE_MAIN_TV_CLA);// 학원전체공지
		school_Tv = (TextView) findViewById(R.id.NOTICE_MAIN_TV_SCH);// 반공지
		school_Tv.setOnClickListener(this);
		class_Tv.setOnClickListener(this);
		findViewById(R.id.NOTICE_MAIN_IV).setOnClickListener(this); // 글쓰기 버튼

		fragment[0] = new Notice_School_F();
		fragment[1] = new Notice_Class_F();

		toolbar = (Toolbar) findViewById(R.id.NOTICE_MAIN_TOOBAR);

		// 원장은 반공지 안보이게.
		class_Tv.setVisibility(View.VISIBLE);
		if (member.getString("Mem_Rank", "").equals("1")) {
			class_Tv.setVisibility(View.INVISIBLE);
		}
		if (member.getString("Mem_Rank", "").equals("3")) {
			findViewById(R.id.NOTICE_MAIN_IV).setVisibility(View.INVISIBLE);
		}
		if (member.getString("Mem_Rank", "0").equals("2")
				|| classcode.equals("2")) {

			school_Tv
					.setBackgroundResource(R.drawable.layout_button_notice_white);
			class_Tv.setBackgroundResource(R.drawable.layout_button_notice);
			checkFrag = 1;
			checkpage = 2;
			getSupportFragmentManager().beginTransaction()
					.replace(R.id.NOTICE_MAIN_LL_FRAGMENT, fragment[1])
					.commit(); // 반별공지
		} else {
			getSupportFragmentManager().beginTransaction()
					.replace(R.id.NOTICE_MAIN_LL_FRAGMENT, fragment[0])
					.commit(); // 기본 학원공지.
		}
	}

	private void setStartToolbar() { // 툴바설정.

		toolbar.setNavigationOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.NOTICE_MAIN_TV_SCH: // 학원공지
			if (checkpage == 2) {
				startDIal();
				school_Tv
						.setBackgroundResource(R.drawable.layout_button_notice);
				class_Tv.setBackgroundResource(R.drawable.layout_button_notice_white);
				checkFrag = 0;
				checkpage = 1;
				getSupportFragmentManager().beginTransaction()
						.replace(R.id.NOTICE_MAIN_LL_FRAGMENT, fragment[0])
						.commitAllowingStateLoss(); // 기본 학원공지.
			}
			break;
		case R.id.NOTICE_MAIN_TV_CLA: // 반공지
			if (checkpage == 1) {
				startDIal();
				school_Tv
						.setBackgroundResource(R.drawable.layout_button_notice_white);
				class_Tv.setBackgroundResource(R.drawable.layout_button_notice);
				checkFrag = 1;
				checkpage = 2;
				getSupportFragmentManager().beginTransaction()
						.replace(R.id.NOTICE_MAIN_LL_FRAGMENT, fragment[1])
						.commitAllowingStateLoss(); // 반공지.
			}
			break;
		case R.id.NOTICE_MAIN_IV: // 글쓰기
			Intent i = new Intent(Notice_Main_A.this,
					Notice_Detail_Edit_A.class).putExtra("mode", "insert");
			startActivityForResult(i, NOTICE_EDIT);
			break;

		}
	}

}// CLASS_END
